<?php
require_once(dirname(__FILE__) . '/../include/config.inc.php');
require_once "function.php";

//工单提交处理
$post_time = time();
$post_time2 = date("Y-m-d H:i:s", time());


$h = date("H", time());
$h = intval($h);
$d = date("d", time());
$d = intval($d);


// 列出系统中 抢单 任务
$sql_order_qiang = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='2' and qiang='q'   and delstate=''   order by id desc   ";
$dosql->Execute($sql_order_qiang);
while ($row_qiang = $dosql->GetArray()) {
    $orderid = $row_qiang['id'];
    $orderidname = date('Ymd', $row_qiang['post_time']) . "-" . $orderid;  //工单号
    $repairname = getRepairMode($row_qiang['repairid']);  //任务名称
    $addtime = $row_qiang['post_time'];
    $apply_time = date("Y-m-d H:i:s", $row_qiang['post_time']);
    //超过指定时间 后进行系统 指派模式
    if ($post_time - $addtime >$cfg_qiangout*60) {
        //列出 工程师 计算忙闲状态
        // 1 获取抢单的工程师

        $qiang_user = array();
        $user_do_num = array();
        $no_working_openid = array();
        $qiang_openid = "";
        $xian_arr = array();
        $xian_openid_arr = array();
        $dosql->Execute("SELECT `openid`,`id`,`truename` FROM  `#@__user`  WHERE level='3' and qiang='1'  ORDER BY id asc");
        while ($row_qiang = $dosql->GetArray()) {

            $user_id = $row_qiang['id'];
            //2 列出处理中的订单
            $do_num_count = 0;
            $do_order = $dosql->GetOne("select count(id) as num from `#@__order`  where jindu<5 and jindu>0  and FIND_IN_SET('$user_id',manageid) and delstate='' ");
            if (is_array($do_order) && isset($do_order['num'])) {
                $do_num_count = $do_order['num'];
            }
            //找出闲人
            if ($do_num_count == 0) {
                $no_working_openid[] = $row_qiang['openid'];
            }
            $row_qiang['do_num_count'] = $do_num_count;
            $user_do_num[] = $do_num_count;
            $qiang_user[] = $row_qiang;

        }
        //对相对空闲的工程师进行派单
        $zuixian = min($user_do_num); //获取最小的处理工单数量
        $xian_arr = array_keys($user_do_num, $zuixian);
        foreach ($xian_arr as $key => $val) {
            $xian_openid_arr[] = $qiang_user[$val];
        }
        //抽取随机人员
        $rndKey = array_rand($no_working_openid);
        $qiang_user = $xian_openid_arr[$rndKey];
        $qiang_openid=$qiang_user['openid'];
        $qiang_user_id=$qiang_user['id'];
        if ($qiang_openid != "") {
            //开始智能分配
            $json2 = '{
							  "touser": "' . $qiang_openid . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，你有系统智能分配的工单请及时处理。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $qiang_user['truename'] . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "系统根据忙闲情况智能分配，请及时接单。", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击接单。如2个小时不接单，系统将扣除奖励1积分。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
            sendWxMsg($json2);
            //修改订单状态
            $dosql->ExecNoneQuery("update  `#@__order` set jindu='4',manageid='$qiang_user_id' where id='orderid' ");
            $dosql->ExecNoneQuery("insert into `#@__order_log`(orderid,op,op_time,jindu,manageid) values ('$orderid','系统智能分配工单','$post_time','4','$qiang_user_id')");
        }


    }
}


//========= 工作时执行 ============
// 列出系统中未接单的 工单  
// 60分钟接单 （每60分钟提醒一次）  2小时后不接单扣除奖励  
if ($h > 7 && $h < 19) {
    $sql_order_2 = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='3'   and delstate=''   order by id desc   ";
    $dosql->Execute($sql_order_2);
    while ($row2 = $dosql->GetArray()) {
        $orderid = $row2['id'];
        $mids = $row2['manageid'];
        $orderidname = date('Ymd', $row2['post_time']) . "-" . $orderid;  //工单号
        $repairname = getRepairMode($row2['repairid']);  //任务名称
        $apply_time = date("Y-m-d H:i:s", $row2['post_time']);

        //查找 任务日志
        $row_order = $dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='3'  order by id desc   ");
        $last_time = $row_order['op_time'];

        //执行人
        $zhixingren = "";
        $ids_arr = explode(",", $mids);
        foreach ($ids_arr as $key => $val) {
            $zhixingren .= getManage($val) . "    ";
        }

        //消息推送
        if ($post_time - $last_time > 3600) {
            //判断是否 推送消息
            $row_timeout = $dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND  jindu='3' AND sendmsg='true'  order by id desc   ");
            if (is_array($row_timeout) && isset($row_timeout['id'])) {
                $last_outtime = $row_timeout['op_time'];
                // 判断是否  再次 超过
                if ($post_time - $last_outtime > 3600) {
                    //再次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单未接单超时提醒','$post_time','3','$val','true')");

                        $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，你有工单未接单请及时处理。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "请及时接单，每60分钟提醒一次。", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击接单。如2个小时不接单，系统将再次扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                        sendWxMsg($json2);


                    }
                }
            } else {
                //首次超时 ，记录入库 并发送消息

                foreach ($ids_arr as $key => $val) {
                    //获取工程师 openid
                    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                    $openid_str = $m['openid'];
                    //插入更新的状态
                    $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单未接单超时提醒','$post_time','3','$val','true')");
                    $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，请及时接单。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "请及时接单，每60分钟提醒一次", 
									  "color": "#173177"
								  },
								  "remark": {
									   "value": "请点击处理，及时接单。如2个小时不接单，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                    sendWxMsg($json2);

                }
            }


            // 扣除奖励
            if ($post_time - $last_time > 7200) {
                //判断是否 推送消息
                $row_fee = $dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'   AND jindu='3'  AND op='move'  order by id desc   ");
                if (is_array($row_fee) && isset($row_fee['id'])) {
                    $last_feetime = $row_fee['op_time'];
                    // 判断是否  再次 超过
                    if ($post_time - $last_feetime > 7200) {
                        //再次超时 ，记录入库 并发送消息
                        foreach ($ids_arr as $key => $val) {
                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //插入更新的状态
                            $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','3','$val','1','工单2小时没有接单，再次扣除奖励1元')");
                            //推送超时提醒

                        }
                    }
                } else {
                    //首次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','3','$val','1','工单2小时没有接单，扣除奖励1元')");
                    }
                }

            }

        }


    }

}


//========= 工作时执行 ============
// 列出系统中 接单未签到 任务   
// 先签到再完成或者反馈  接单2小时后签到 
if ($h > 7 && $h < 19) {


    $sql_order = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='4'   and delstate=''  and qiandao=''  order by id desc   ";
    $dosql->Execute($sql_order);
    while ($row = $dosql->GetArray()) {
        $orderid = $row['id'];
        $mids = $row['manageid'];
        $orderidname = date('Ymd', $row['post_time']) . "-" . $orderid;  //工单号
        $repairname = getRepairMode($row['repairid']);  //任务名称
        $apply_time = date("Y-m-d H:i:s", $row['post_time']);
        //执行人
        $zhixingren = "";
        $ids_arr = explode(",", $mids);
        foreach ($ids_arr as $key => $val) {
            $zhixingren .= getManage($val) . "    ";
        }
        //查找 接单 日志
        // 如果存在反馈的信息，则不执行
        $row_order = $dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='4'   order by id desc   ");
        $last_time = $row_order['op_time'];
        $is_fankui = $row_order['fankui'];


        //接单后需要签到的
        //消息推送
        if ($post_time - $last_time > 7200 && $is_fankui == '') {
            //判断是否 推送消息
            $row_timeout = $dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND   jindu='4' AND  sendmsg='true'  order by id desc   ");
            if (is_array($row_timeout) && isset($row_timeout['id'])) {
                $last_outtime = $row_timeout['op_time'];
                // 判断是否  再次 超过
                if ($post_time - $last_outtime > 7200) {
                    //再次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单超时提醒','$post_time','4','$val','true')");

                        $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，接单后请及时签到和处理。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "接单后2个小时没有签到，再次超时", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击处理，请及时接单。如60分钟后不接单，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                        sendWxMsg($json2);


                    }
                }
            } else {
                //首次超时 ，记录入库 并发送消息

                foreach ($ids_arr as $key => $val) {
                    //获取工程师 openid
                    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                    $openid_str = $m['openid'];
                    //插入更新的状态
                    $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','工单超时提醒','$post_time','4','$val','true')");
                    $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，接单后请及时签到和处理。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "接单后2个小时没有签到", 
									  "color": "#173177"
								  },
								  "remark": {
									   "value": "请点击处理，请及时签到。如60分钟后不签到，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                    sendWxMsg($json2);

                }
            }

            // 扣除奖励
            if ($post_time - $last_time > 10800) {
                //判断是否 推送消息
                $row_fee = $dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'  AND jindu='4'  AND op='move'  order by id desc   ");
                if (is_array($row_fee) && isset($row_fee['id'])) {
                    $last_feetime = $row_fee['op_time'];
                    // 判断是否  再次 超过
                    if ($post_time - $last_feetime > 10800) {
                        //再次超时 ，记录入库 并发送消息
                        foreach ($ids_arr as $key => $val) {
                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //插入更新的状态
                            $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','工单超时没有接单，再次扣除奖励1元')");
                            //推送超时提醒

                        }
                    }
                } else {
                    //首次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','工单超时没有接单，扣除奖励1元')");
                    }
                }

            }

        }

        //接单后  反馈  然后  第二天需要重新签到的
        //消息推送
        $last_time_d = date("d", $last_time);
        if ($last_time_d != $d && $is_fankui == 'true') {
            //判断是否 推送消息
            $row_timeout = $dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND   jindu='4' AND  sendmsg='true'  order by id desc   ");
            if (is_array($row_timeout) && isset($row_timeout['id'])) {
                $last_outtime = $row_timeout['op_time'];
                // 判断是否  再次 超过
                if ($post_time - $last_outtime > 3600) {
                    //再次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','反馈工单次日再次超时提醒','$post_time','4','$val','true')");

                        $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，昨日反馈工单请及时签到和反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "昨天反馈的工单，今天需要重新签到。再次提醒", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击处理，然后签到，如约定其他时间请再次反馈。如90分钟后不处理，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                        sendWxMsg($json2);
                    }
                }
            } else {
                //首次超时 ，记录入库 并发送消息
                foreach ($ids_arr as $key => $val) {
                    //获取工程师 openid
                    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                    $openid_str = $m['openid'];
                    //插入更新的状态
                    $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg) values ('$orderid','反馈工单次日超时提醒','$post_time','4','$val','true')");
                    $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，昨日反馈工单请及时签到和反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "昨天反馈的工单，今天需要重新签到。首次提醒", 
									  "color": "#173177"
								  },
								  "remark": {
									   "value": "请点击处理，然后签到，如约定其他时间请再次反馈。如90分钟后不处理，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                    sendWxMsg($json2);

                }
            }

            // 扣除奖励
            // 上班时间
            $workToday = mktime(8, 00, 0, date('m'), date('d'), date('Y'));  //今日上班开始时间
            if ($post_time - $workToday > 5400) {
                //判断是否 推送消息
                $row_fee = $dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'  AND jindu='4'  AND op='move'  order by id desc   ");
                if (is_array($row_fee) && isset($row_fee['id'])) {
                    $last_feetime = $row_fee['op_time'];
                    // 判断是否  再次 超过
                    if ($post_time - $last_feetime > 5400) {
                        //再次超时 ，记录入库 并发送消息
                        foreach ($ids_arr as $key => $val) {
                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //插入更新的状态
                            $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','反馈工单次日未签到或反馈，再次扣除奖励1元')");
                            //推送超时提醒

                        }
                    }
                } else {
                    //首次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark) values ('$orderid','move','$post_time','4','$val','1','反馈工单次日未签到或反馈，扣除奖励1元')");
                    }
                }

            }

        }
    }
}


//========= 下班执行  ============
// 列出系统中 签到后 未处理的数据
if ($cfg_xia == "Y") {
    $beginToday = mktime(18, 30, 0, date('m'), date('d'), date('Y'));  //下班开始时间
}
if ($cfg_xia == "N") {
    $beginToday = mktime(18, 00, 0, date('m'), date('d'), date('Y'));  //下班开始时间
}


$endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1; //今天的结束时间
if ($post_time > $beginToday && $post_time < $endToday) {
    echo "下班执行开始";

    $sql_order_2 = "SELECT id,manageid,post_time,repairid  FROM  `#@__order`  where    jindu='4'   and  delstate=''  and qiandao='true'  order by id desc   ";
    $dosql->Execute($sql_order_2);
    while ($row2 = $dosql->GetArray()) {
        $orderid = $row2['id'];
        $mids = $row2['manageid'];
        $orderidname = date('Ymd', $row2['post_time']) . "-" . $orderid;  //工单号
        $repairname = getRepairMode($row2['repairid']);  //任务名称
        $apply_time = date("Y-m-d H:i:s", $row2['post_time']);

        //查找 任务日志
        $row_order = $dosql->GetOne("SELECT *  FROM  `#@__order_log`  where   orderid='$orderid'   AND jindu='4'   order by id desc   ");
        if (is_array($row_order) && $row_order['qiandao'] == 'true') {
            $last_time = $row_order['op_time'];

            //执行人
            $zhixingren = "";
            $ids_arr = explode(",", $mids);
            foreach ($ids_arr as $key => $val) {
                $zhixingren .= getManage($val) . "    ";
            }

            //消息推送
            //判断是否 推送消息
            $row_timeout = $dosql->GetOne("SELECT *  FROM  `#@__timeout_log`  where   orderid='$orderid'   AND  jindu='4' AND sendmsg='true'  AND qiandao='1' order by id desc   ");
            if (is_array($row_timeout) && isset($row_timeout['id'])) {
                $last_outtime = $row_timeout['op_time'];
                // 判断是否  再次 超过
                if ($post_time - $last_outtime > 1800) {
                    //再次超时 ，记录入库 并发送消息
                    foreach ($ids_arr as $key => $val) {
                        //获取工程师 openid
                        $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                        $openid_str = $m['openid'];
                        //插入更新的状态
                        $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg,qiandao) values ('$orderid','下班签到工单未处理，再次提醒','$post_time','4','$val','true','1')");

                        $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，辛苦一天下班了，你有工单还没有完成或反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "如任务结束请进行完修申报，如未结束请及时反馈。", 
									  "color": "#173177"
								  },
								  "remark": {
									  "value": "请点击处理。如90分钟后未处理，系统将再次扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                        sendWxMsg($json2);


                    }
                }
            } else {
                //首次超时 ，记录入库 并发送消息

                foreach ($ids_arr as $key => $val) {
                    //获取工程师 openid
                    $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                    $openid_str = $m['openid'];
                    //插入更新的状态
                    $dosql->ExecNoneQuery("insert into `#@__timeout_log`(orderid,op,op_time,jindu,manageid,sendmsg,qiandao) values ('$orderid','下班后签到工单未处理，首次提示','$post_time','4','$val','true','1')");
                    $json2 = '{
							  "touser": "' . $openid_str . '" ,  
							  "template_id": "'.$cfg_order_status.'",    
							  "url":"'.$cfg_weburl.'wx/morderDetail.php?orderid=' . $orderid . '",  
							  "data": {
								  "first": {
									  "value": "您好，辛苦一天下班了，你有工单还没有完成或反馈。", 
									  "color": "#f71b03"
								  }, 
								  "keyword1": {
									  "value": "' . $orderidname . '", 
									  "color": "#173177"
								  }, 
								  "keyword2": {
									  "value": "' . $apply_time . '", 
									  "color": "#173177"
								  },
								  "keyword3": {
									  "value": "' . $zhixingren . '", 
									  "color": "#0e48cb"
								  },
								  "keyword4": {
									  "value": " ' . $repairname . '", 
									  "color": "#173177"
								  },
								  "keyword5": {
									  "value": "如任务结束请进行完修申报，如未结束请及时反馈。", 
									  "color": "#173177"
								  },
								  "remark": {
									   "value": "请点击处理。如90分钟后不处理，系统将扣除奖励1元。服务热线：'.$cfg_hotline.'", 
									  "color": "#f71b03"
								  } 
							  }
							}';
                    sendWxMsg($json2);

                }
            }


            // 扣除奖励
            if (is_array($row_timeout) && isset($row_timeout['id'])) {
                $last_outtime = $row_timeout['op_time'];
                if ($post_time - $last_outtime > 1900) {
                    //是否扣费
                    $row_fee = $dosql->GetOne("SELECT *  FROM  `#@__fee_log`  where   orderid='$orderid'   AND jindu='4'  AND op='move' and qiandao='1'  order by id desc   ");
                    if (is_array($row_fee) && isset($row_fee['id'])) {
                        $last_feetime = $row_fee['op_time'];
                        // 判断是否  再次 超过
                        if ($post_time - $last_feetime > 7200) {
                            //再次超时 ，记录入库 并发送消息
                            foreach ($ids_arr as $key => $val) {
                                //获取工程师 openid
                                $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                                $openid_str = $m['openid'];
                                //插入更新的状态
                                $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,qiandao) values ('$orderid','move','$post_time','4','$val','1','工单2小时没有处理，再次扣除奖励1元，再次','1')");
                                //推送超时提醒

                            }
                        }
                    } else {
                        //首次超时 ，记录入库 并发送消息
                        foreach ($ids_arr as $key => $val) {
                            //获取工程师 openid
                            $m = $dosql->GetOne("select openid  from `#@__user`  where id='$val'");
                            $openid_str = $m['openid'];
                            //插入更新的状态
                            $dosql->ExecNoneQuery("insert into `#@__fee_log`(orderid,op,op_time,jindu,manageid,fee,remark,qiandao) values ('$orderid','move','$post_time','4','$val','1','工单2小时没有处理，扣除奖励1元，首次','1')");
                        }
                    }

                }
            }

        }
    }
}

?>